function fent = nlev_u(nfield,Afield) 
%clear all
np= 4;
NT = 10000;
j = ((np)-1)/2;
m = -j;
jy = 0;
jz = 0;
          for ii=1:np-1
          jz2(ii) = m^2;
          jz(ii,ii) = m;
          jnoz(ii) = sqrt(j*(j+1)-m*(m+1));
          jy(ii,ii+1) = jnoz(ii)*i;
          jy(ii+1,ii) = -jnoz(ii)*i;
          jx(ii,ii+1) = jnoz(ii);
          jx(ii+1,ii) = jnoz(ii);
          m = m+1;
          end  
          jz2(np) = j^2;
          jz(np,np) = j;
          jy=jy/2;
          jx = jx/2;
          rrho0 = zeros(2);
          rrho0(1,1) = 1.;
        
%      parameters
         delta0 = 3.e-3;  % energy difference
         
         uz2 = 2*delta0/j;
          lam = 1.e1; % field constraint
          fieldcon = 0;
%          finding evecs & evals for H0:
          H0 = 0;
%          H0op = trace(delta0*jz*rrho0);
%          L0op = trace(-delta0*jx*rrho0);
%          C0op = trace(jy*rrho0);
        
         H0 = uz2*diag(jz2)+ delta0*jx;
         
          %for ii=1:np-1
         % H0(ii,ii) = uz2*jz2(ii);
         % H0(ii,ii+1) = delta0*jnoz(ii);
         % H0(ii+1,ii) =  H0(ii,ii+1)';
         % end 
         % H0(np,np) = uz2*jz2(np);
 %        H0 = delta0*jz;
       % call diagu(np,H0,Uop1,E0)
       [Uop1,E0 ] = eig(H0);
       E0=diag(E0);
       jz = Uop1*jz*Uop1';
       jy = Uop1*jy*Uop1';
       %jx = Uop1*jx*Uop1';
       
       %            jz = matmul(matmul(H0,jz),conjg(transpose(H0)))
%            jy = matmul(matmul(H0,jy),conjg(transpose(H0)))
           
        beta = 1/delta0;   % Temperature
        bathcoup = 1e4;  % e6 for decay to equiv. 
        bpower = 3; % power of omega. depend on the bath model. 3 for the vacuum.
%      light params
        twidth = 1.e-14/2.4e-17 ;
        trun = 2e0*twidth;
        tcenter = trun/2;
        t = trun/(NT-1);
        om0 = 3*2*pi/trun;
        mu = -om0/delta0;
        smu = sqrt(mu^2+1);
        
%      inertial parameters:
        inrtmu0 = -0.5e0;
        inrtalpha0 = pi*1e-8;
        inrtdelta = 0 ; %-inrtalpha0       
        inrtgamma = 0*inrtalpha0; %
 %       load infile
 %       nfield = infile(1);
 %       Afield = infile(2:nfield+1);
 %      Afield=0.;
        U0 = eye(np);
%       U0(2,2) = exp(zi*1d-10) !-1.d0

        nch = 10  ;

%         system-bath coupling operator, currently \sigma_x
         coupop = 0;
         coupop = jy;
%        coupop(1,2) =  1.d0
%        coupop(2,1) =  1.d0

%        time propagation
        time = 0 ;
        l=1 ;
        Uop = U0 ;
           
%       density operator, in the interaction picture:
% eq. relations:
% Pg/Pe = 1+exp(beta*delta-1)
         rho = zeros(2) ;
         rho1 = zeros(2);
         norm= 0 ;
         ent0 = 0 ;
          for ii=1:np
        rho(2,2) = 1.;
        rho1(1,1) = 1.;
%        rho(2,2) = exp(-beta*delta0);
 %         rho(ii,ii) = exp(-beta*E0(ii));
 %         norm = norm+rho(ii,ii);
          end 
%         rho = rho/norm;
         for ii=1:np
          ent0 = ent0-rho(ii,ii)*log(rho(ii,ii));
         end 
         hadamard = 1/sqrt(2)*( [1 1 ; 1 -1]);       
          rhof = hadamard*rho*hadamard;
          rhof1 = hadamard*rho1*hadamard;
        emax = 0;
        omega(1:np*np) = 0;
        epsi(1:np) = 0;
        cvls(1:np,1) =1 ;
        while (time<trun)
          fieldeps= 0; %delta0*sin(time*om0);
          fieldom = delta0; %*cos(time*om0);
          for ii=1:nfield
         fieldeps = fieldeps+Afield(ii)*sin(time*ii*pi/trun);
          end 
          fieldeps = fieldeps*exp(-((time-tcenter)/twidth)^2);
          field(l) = fieldeps;
%          if (abs(fieldeps).lt.1.e-80) fieldeps = 0.d0
 %          Hop = fieldom*jz+fieldeps*jx;
 %          Lop = fieldeps*jz-fieldom*jx;
 %          Cop = jy;
 %          theta = delta0*time;
           
            [Uop,emax] = propag(Uop,t,nch,np,fieldeps,fieldom,emax,E0,jx,jz);
                Uop2 = Uop;
                unita(l) = trace(Uop*Uop');
%                expectx(l) = trace(Uop*jx*Uop'*rrho0);
%                expecty(l) = trace(Uop*jy*Uop'*rrho0);
%                expectz(l) = trace(Uop*jz*Uop'*rrho0);
%                expectH(l) = real(trace(Uop'*Hop*Uop*rrho0));
%                expectL(l) = real(trace(Uop'*Lop*Uop*rrho0));
%                expectC(l) = real(trace(Uop'*Cop*Uop*rrho0));
%                Hout(l) = (mu^2*cos(theta*smu)+1)/smu^2*H0op;
%                Hout(l) = Hout(l) + mu*sin(theta*smu)/smu*L0op;
%                Hout(l) = Hout(l) + mu*(1-cos(theta*smu))/smu^2*C0op;
%                Lout(l) = -mu*sin(theta*smu)/smu*H0op;
%                Lout(l) = Lout(l) + cos(theta*smu)*L0op;
%                Lout(l) = Lout(l) + sin(theta*smu)/smu*C0op;
%                Cout(l) = mu*(1-cos(theta*smu))/smu^2*H0op;
%                Cout(l) = Cout(l) - sin(theta*smu)/smu*L0op;
%                Cout(l) = Cout(l) +  (cos(theta*smu)+mu^2)/smu^2*C0op;
%               Uop1 = matmul(Uop1,Uop2)
%               cnorm = trace(2,matmul(Uop1,transpose(conjg(Uop1))))
%            write(17,112) time, real(trace(2,matmul(Uop2,transpose(conjg(Uop2))))),fieldeps
%            write(17,112) real(time,4), real(abs(Uop2(1,1))**2,4),real(abs(Uop1(2,1))**2,4), real(fieldom,4)
%            write(18,112) real(time,4), real(abs(Uop2(2,2))**2,4),real(abs(Uop1(1,2))**2,4), real(fieldeps,4)
%        call diagu(np,Uop2,Uop1,evls)
        [Uop2,evls] = eig(Uop2);
        evls= diag(evls);
        
     
  %           epsi = atan(imag(evls)./(real(evls)));
    
%        arranging the right order of evls and efuncs
           if (l > 1)
            for ii1=1:np
            psi = Uop2(:,ii1);
             spot = 0;
             movlp = 0.;
             for ii=1:np
              psi1 = OldU(:,ii);
             cnorm =  psi'*psi1;
              if (abs(cnorm)>movlp) 
              spot = ii;
              movlp = abs(cnorm);
              end
             end 
             Uop1(:,spot) = Uop2(:,ii1);
             auxevls(spot) = evls(ii1);
            end 
%           Uop  = Uop1
            Uop2 = Uop1;
              
               cvls(:,l) = auxevls;
                    for ii=1:np
     %      epsil(ii,1:l) = phase(cvls(ii,1:l));
     %      epsil(ii,l) = real(i*log(cvls(ii,l)));
             epsil(ii,l) = oldepsi(ii)-real(i*diff(cvls(ii,l-1:l))/cvls(ii,l));
                    end
         epsi = epsil(:,l);
               
            end
            oldepsi = epsi;
            oldomega = omega;
            OldU = Uop2;
            epsiout(:,l) = epsi;
%      write(4000,112) epsi
%         creating the complete set of operators and evls:
            iii=1;
              UT = Uop2';
              for ii1=1:np
                for ii=1:np
                trow = Uop2(1:np,ii);
                tcol = UT(ii1,1:np);
                obasis(:,:,iii) = trow*tcol;
                
%      write(2000+iii,112) real(obasis(:,:,iii))
%      write(3000+iii,112) aimag(obasis(:,:,iii))

               omega(iii) = epsi(ii1)-epsi(ii);
               alpha(iii) = (omega(iii)-oldomega(iii))/t;
               nnumber(iii) = 0.;
               if (abs(alpha(iii))>1d-12)
%              nnumber(iii) = (exp(beta*abs(alpha(iii))-1.d0))**(-1)
               nnumber(iii) = (exp(beta*abs(alpha(iii)))-1)^(-1);
%                 if (alpha(iii).lt.0.) nnumber(iii)=nnumber(iii)+1.d0
                  if (alpha(iii) < 0 ) 
                      nnumber(iii)=nnumber(iii)+1.d0;
                  end
               nnumber(iii) = nnumber(iii)*bathcoup*abs(alpha(iii))^bpower;
               end 
                iii=iii+1;
                end 
                end
     
         
%        checking completness:
%            do i=1,np*np
%               Uop1 = obasis(:,:,i,l)
%               Uop1 = transpose(conjg(Uop1))
%            do ii=1,np*np
%               Uop2 = matmul(obasis(:,:,ii,l),Uop1)
%               comp(i,ii,l) = trace(np,Uop2)
%            end do
%            end do
%              call random_number(rnums)
%            psi = rnums(1:np*np) + zi*rnums(np*np+1:2*np*np)
%                       write(16,*) real(time,4), real(cnorm)
%            call vprod(psi,psi,np*np,cnorm)
%           psi = psi/sqrt(abs(cnorm))
%            call vprod(psi,psi,np*np,cnorm)
%           psi1 = matmul(comp(:,:,l),psi)
%            call vprod(psi1,psi,np*np,cnorm)
             
%          operator expansion coeffcients:
            for ii=1:np*np
            UT = obasis(:,:,ii)';
            coef(ii) = abs(trace(UT*coupop))^2;
            coef(ii) = coef(ii)*nnumber(ii);
            end
            
            gamax = abs(max(coef));
            if abs(gamax<1e-12)
                gamax = emax/1.e4;
            end

          rho = inprop(emax,gamax,np,t,rho,obasis,coef);     
          rho1 = inprop(emax,gamax,np,t,rho1,obasis,coef);
    
            rhosch = Uop*rho*Uop';
            rhosch1 = Uop*rho1*Uop';
            

%            entropy 
            temprho = rho;
            rhoeig=eig(temprho);
            entropy(l) = -sum(real(rhoeig).*log(real(rhoeig)));
            temprho = rho1;
            rhoeig=eig(temprho);
            entropy1(l) = -sum(real(rhoeig).*log(real(rhoeig)));
            
            for iii=1:np
             pop(iii,l) = real(rhosch(iii,iii));
             pop1(iii,l) = real(rhosch1(iii,iii));
            end 
            coher(l) = rhosch(1,2);
            coher1(l) = rhosch1(1,2);
            fid(l) = real(trace(rhosch'*rhof));
            fid(l) = fid(l) + real(trace(rhosch1'*rhof));
    %        fieldcon = fieldcon+lam*fieldeps^2;

        time=time+t ;
        l=l+1;
              end 
        %fent = entropy(end);
     fent = real(trace(rhosch'*rhof));
     %fent = fent + real(trace(rhosch1'*rhof));
     fent = fent + real(trace(rhosch1'*rhof1));
     2-fent;
%     1-fent ;    
 %          write(666,112) entropy+fieldcon
